# _*_ coding: utf-8 _*_
from odoo import models, fields, api


class CreateRequestOrder(models.TransientModel):
    _name = "create.purchase.request.order.wizard"

    supplier_id = fields.Many2one("res.partner", string="Supplier", domain=[("supplier", "=", True)])

    def action_ok(self):
        self.ensure_one()
        lines = self.env["purchase.requisition.line"].browse(self.env.context.get("active_ids"))
        if lines:
            vals = {
                "supplier_id": self.supplier_id.id
            }
            line_vals = [(0, 0, {
                "product_id": l.product_id.id,
                "product_uom_id": l.product_uom_id.id,
                "price_unit": l.price_unit,
                "product_qty": l.product_qty,
                "delivery_date": fields.Date.today(),
                "requisition_line_id": l.id
            }) for l in lines if l.state == "assign"]
            vals["line_ids"] = line_vals
            order = self.env["purchase.request.order"].create(vals)
            return {
                'type': 'ir.actions.act_window',
                'res_model': order._name,
                'res_id': order.id,
                'view_type': 'form',
                'view_mode': 'form',
                'target': 'current'
            }
